# -*- coding:utf-8 -*-
import json
import xlrd
from xlutils.copy import copy
import requests
import time
from util.get_casedata import GetCaseData


class GetResponseData:
    def __init__(self, file_path):
        self.file_path = file_path
        self.get_case_data = GetCaseData(self.file_path)
        self.run_config = self.get_case_data.get_common_data()

    def send_request(self, method, url, data=None, params=None, headers=None,
                     cookies=None, json=None, files=None, timeout=None):
        resoponse = requests.request(method, url, data=data, params=params, headers=headers,
                                     cookies=cookies, json=json, files=files, timeout=timeout)

        res = {}
        res['status_code'] = resoponse.status_code
        res['content'] = resoponse.text
        res['time'] = resoponse.elapsed.total_seconds()

        return res

    def write_result(self, file_path, caseid, result):
        case = self.get_case_data.get_case(caseid)
        if not isinstance(result, dict):
            json.loads(result)

        workbook = xlrd.open_workbook(file_path)
        new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
        new_worksheet = new_workbook.get_sheet(case['sheet_name'])

        new_worksheet.write(case['row'], 10, result['case_result'])
        new_worksheet.write(case['row'], 11, result['case_actual'])
        new_worksheet.write(case['row'], 12, result['case_exectime'])

        new_workbook.save(self.file_path)

    def report(self, file_path):
        workbook = xlrd.open_workbook(file_path)
        new_workbook = copy(workbook)  # 将xlrd对象拷贝转化为xlwt对象
        new_worksheet = new_workbook.get_sheet('config')

        run_date = time.strftime("%Y-%m-%d", time.localtime())
        run_time = 0
        case_num = 0
        pass_num = 0
        failed_num = 0
        row = 13

        for sheet in workbook.sheets():
            if sheet.name == 'config':
                url = sheet.cell_value(0, 1)
            else:
                num = sheet.nrows - 1
                case_num = case_num + num
                T = 0
                F = 0
                if num == 0:
                    T_ratio = '0'
                else:
                    T_ratio = "%.2f%%" % (T/num*100)
                cost = 0
                if sheet.nrows != 1:
                    for r in range(1, sheet.nrows):
                        result = sheet.cell_value(r, 11)
                        if result == 'PASS':
                            T = T + 1
                        elif result == 'FAILED':
                            F = F + 1
                        else:
                            pass
                        t = float(sheet.cell_value(r, 12))
                        cost = cost + t

                new_worksheet.write(row, 0, sheet.name)
                new_worksheet.write(row, 1, num)
                new_worksheet.write(row, 2, T)
                new_worksheet.write(row, 3, F)
                new_worksheet.write(row, 4, T_ratio)
                row = row + 1

                case_num = case_num + num
                pass_num = pass_num + T
                failed_num = failed_num + F
                run_time = run_time + cost
        if case_num == 0:
            fail_ratio = '0'
        else:
            fail_ratio = "%.2f%%" % (failed_num/case_num*100)
        new_worksheet.write(4, 0, '运行日期')
        new_worksheet.write(4, 1, run_date)
        new_worksheet.write(5, 0, '运行耗时')
        new_worksheet.write(5, 1, run_time)
        new_worksheet.write(6, 0, '测试环境')
        new_worksheet.write(6, 1, url)
        new_worksheet.write(7, 0, '执行用例总数')
        new_worksheet.write(7, 1, case_num)
        new_worksheet.write(8, 0, '通过用例数')
        new_worksheet.write(8, 1, pass_num)
        new_worksheet.write(9, 0, '失败用例数')
        new_worksheet.write(9, 1, failed_num)
        new_worksheet.write(10, 0, '失败率')
        new_worksheet.write(10, 1, fail_ratio)
        new_worksheet.write_merge(11, 0, 4, '详细统计')
        new_worksheet.write(12, 0, '模块')
        new_worksheet.write(12, 1, '总用例数')
        new_worksheet.write(12, 2, '成功用例数')
        new_worksheet.write(12, 3, '失败用例数')
        new_worksheet.write(12, 4, '成功率')

        new_workbook.save(self.file_path)

